//
//  CarNoController.m
//  PropertyMananger
//
//  Created by bh on 17/4/5.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "CarNoController.h"
#import "MCSearchBar.h"
#import "CarNumQueryModel.h"

@interface CarNoController ()
//房屋信息数据源
@property (nonatomic,strong) NSMutableArray * carNumberArr;
@end

@implementation CarNoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleViewName:@"请选择车牌号"];
    
    [self setupSearchBar];
    
    
    self.searchStr = @"";
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 50, BHscreenW, BHscreenH - 64 - 50) isGroup:NO];
    
    self.carNumberArr = [[NSMutableArray alloc] init];
    
    [self postCarNumData];
}
#pragma mark - 搜索框
- (void)setupSearchBar
{
    self.searchBar = [[MCSearchBar alloc] initWithFrame:CGRectMake(15, 7.5, BHscreenW - 30, 35) placeholder:@"请输入车牌号"];
    self.searchBar.delegate = self;
    self.searchBar.barTintColor = [UIColor whiteColor];
    [self.view addSubview:self.searchBar];
    [self.searchBar setBackgroundColor:[UIColor whiteColor]];
    self.searchBar.layer.cornerRadius = 5.0f;
    self.searchBar.layer.masksToBounds = YES;
    self.searchBar.layer.borderWidth = 0.5;
    self.searchBar.layer.borderColor = BHColor(102, 102, 102).CGColor;
    self.searchBar.backgroundColor = [UIColor whiteColor];
    self.searchBar.searchTextField.backgroundColor = [UIColor whiteColor];
    self.searchText = @"";
    
    self.searchBar.searchBarStyle = UISearchBarStyleDefault;
    
    self.resultArr = [[NSMutableArray alloc]init];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.isSearch) {
        return self.resultArr.count ;
    }else{
        return self.carNumberArr.count;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textcell"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"textcell"];
        
    }
    
    CarNumQueryModel * model = self.carNumberArr[indexPath.row];
    
    if (self.isSearch == YES) {
        
        cell.textLabel.text = self.resultArr[indexPath.row];
    }else{
        
        cell.textLabel.text = [NSString stringWithFormat:@"车牌号:%@",model.CarNo];
        
        cell.textLabel.textColor = BHColor(51, 51, 51);
        cell.textLabel.font = BHFont(15);
    }
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CarNumQueryModel * model = self.carNumberArr[indexPath.row];
    
    self.searchBar.text = nil;
    if (![self.searchStr isEqualToString:@""]) {
        
        self.searchBar.text = self.resultArr[indexPath.row];
        
    }else{
        
        self.searchBar.text = model.CarNo;
        
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedCarNum:)]) {
        
        [self.delegate didSelectedCarNum:self.searchBar.text];
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
    
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    if (searchText.length == 0) {
        self.searchText = @"";
        self.isSearch = NO;
        [self.tableView reloadData];
    }else{
        
        [self.tableView.mj_header beginRefreshing];
    }
    
    NSLog(@" --- %@",searchText);
    self.searchStr = searchText;
    
    [self.resultArr removeAllObjects];
    
    
    for ( CarNumQueryModel * model in self.carNumberArr) {
        if ([model.RoomName rangeOfString:searchText].location != NSNotFound) {
            [self.resultArr addObject:model.RoomName];
        }
    }
    if (self.resultArr.count) {
        self.isSearch = YES;
        [self.tableView reloadData];
    }
    
}


-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    [self.view endEditing:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.isSearch = NO;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.tableView endEditing:YES];
    [self.view resignFirstResponder];
    [self.view endEditing:YES];
    self.isSearch = NO;
    //  [self.tableView reloadData];
}
#pragma mark - 获取房屋信息
- (void)postCarNumData
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"努力加载数据中...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    
    NSDictionary * parms = nil;
    parms = @{
              @"CommunityID":userModel.CommunityID,
              @"CarNO":self.searchStr,
              @"uid":userModel.WorkerID,
              @"json":@"\{\"jsonEntity\":\{},\"action\":\"getcarinfo\",\"version\":\"V1.0\"}"
              };
    
    DLog(@"车牌号查询提交参数 == %@",parms);
    [HomeNetWorkRequest postCarNumQuerySuccess:^(id data) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        [self.carNumberArr removeAllObjects];
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"车牌号信息的数据 == %@",array);
        
        for (NSDictionary * dict in array) {
            CarNumQueryModel * model = [[CarNumQueryModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.carNumberArr addObject:model];
            DLog(@"车牌号 == %@",model.CarNo);
        }
        
        if (self.carNumberArr.count != 0) {
            self.placehoderImg.hidden = YES;
            self.titleLabel.hidden = YES;
        }
        
        [self.tableView reloadData];
        
    } Fail:^(NSString *errorDes) {
        [MBProgressHUD hideHUD];
    } param:parms];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
